public class Solution623 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public TreeNode addOneRow(TreeNode root, int val, int depth) {
        if (depth==1){
            return new TreeNode(val,root,null);
        }
        dfs(root,depth,val,1);
        return root;
    }

    private void dfs(TreeNode root, int depth, int val, int deep) {
        if (root==null){
            return;
        }
        if (deep+1==depth){
            root.left= new TreeNode(val,root.left,null);
            root.right=new TreeNode(val,null,root.right);
            return;
        }
        dfs(root.left,depth,val,deep+1);
        dfs(root.right,depth,val,deep+1);
    }

    public static void main(String[] args) {
        System.out.println(new Solution623().addOneRow(new TreeNode(4,new TreeNode(2,new TreeNode(3),new TreeNode(1)),new TreeNode(6,new TreeNode(5),null)),1,2).right.val);
    }
}
